<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * create category browse page
 * get: category.tpl.php
 * used from category.page.php
 *
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2012
 * @author     Norbert Gebert
 * @license    GPL3
 */
$_GET['id'] = $refValidate->cleanslash($_GET['id']);

if (isset($_GET['optid']))
{
	if ($refValidate->cleanslash($_GET['subfunc']) === 'delete')
	{
		db::getInstance()->where('options_id', $refValidate->cleanslash($_GET['optid']));

		if (db::getInstance()->delete('options'))
		{
			message::setNoteRow(_('Option successful deleted.'), TRUE);
		}
		else
		{
			message::setErrorRow(_('Option could not be deleted!'), TRUE);
		}
	}
	$pagefile = 'module/page/' . $page . '/options/options.page.edit.php';
	if (is_file($pagefile))
		include_once $pagefile;
}

if (isset($_REQUEST['submit']))
{
	if ($refValidate->cleanslash($_REQUEST['submit']) === _('Cancel'))
	{ // Cancel = browse attribute
		$pagefile = 'module/page/' . $page . '/attribute.page.table.php';
		if (is_file($pagefile))
			include_once $pagefile;
	}
	elseif ($refValidate->cleanslash($_REQUEST['submit']) === _('New'))
	{ // Create new Attribute
		$datax = $refValidate->testText($_POST['optionartx'],	_('Optionart'), 1);
		if (!$datax)
		{
			message::setErrorRow(_('Selected Option not found.'));
		}
		else
		{
			switch ($datax)
			{
				case '1': // Textrow
					$data = Array(
					 'options_attribute_id' => $_GET['id'],
					 'options_name' => _('Your Textrowname'),
					 'options_art' => '1',
					 'options_order' => 0,
					 'options_suborder' => 0,
					 'options_active' => 0,
					 'options_width' => 50
					);
					$id = db::getInstance()->insert('options', $data);
					break;
				case '2': // Textfield
					$data = Array(
					 'options_attribute_id' => $_GET['id'],
					 'options_name' => _('Your Textfieldname'),
					 'options_art' => '2',
					 'options_order' => 0,
					 'options_suborder' => 0,
					 'options_active' => 0,
					 'options_width' => 35,
					 'options_height' => 10
					);
					$id = db::getInstance()->insert('options', $data);
					break;
				case '3': // Drop-Down
					$data = Array(
					 'options_attribute_id' => $_GET['id'],
					 'options_name' => _('Your Dropdownfieldname'),
					 'options_art' => '3',
					 'options_order' => 0,
					 'options_suborder' => 0,
					 'options_active' => 0,
					 'options_width' => 50
					);
					$id = db::getInstance()->insert('options', $data);
					break;
				case '4': // Option-List
					$data = Array(
					 'options_attribute_id' => $_GET['id'],
					 'options_name' => _('Your Optionsfieldname'),
					 'options_art' => '4',
					 'options_order' => 0,
					 'options_suborder' => 0,
					 'options_active' => 0,
					 'options_width' => 50
					);
					$id = db::getInstance()->insert('options', $data);
					break;
			}
		}
		$pagefile = 'module/page/' . $page . '/options/options.page.edit.php';
		if (is_file($pagefile))
			include_once $pagefile;
	}
	elseif ($refValidate->cleanslash($_REQUEST['submit']) === _('Save changes'))
	{
		db::getInstance()->where('options_attribute_id', $_GET['id']);
		db::getInstance()->orderBy('options_order', 'ASC');
		db::getInstance()->orderBy('options_suborder', 'ASC');
		$result = db::getInstance()->get('options');

		foreach ($result as $row)
		{
			switch ($row['options_art'])
			{
				case '1': // Textrow
					$data = Array(
					 'options_titel' => $_POST['titelx' . $row['options_id']],
					 'options_text' => $_POST['textx' . $row['options_id']],
					 'options_width' => $_POST['widthx' . $row['options_id']],
					 'options_order' => $_POST['positionx' . $row['options_id']]
					);
					db::getInstance()->where('options_id', $row['options_id']);
					db::getInstance()->update('options', $data);
					break;
				case '2': // Textfield
					$data = Array(
					 'options_titel' => $_POST['titelx' . $row['options_id']],
					 'options_text' => $_POST['textx' . $row['options_id']],
					 'options_width' => $_POST['widthx' . $row['options_id']],
					 'options_height' => $_POST['heightx' . $row['options_id']],
					 'options_order' => $_POST['positionx' . $row['options_id']]
					);
					db::getInstance()->where('options_id', $row['options_id']);
					db::getInstance()->update('options', $data);
					break;
				case '3': // Drop-Down
					$data = Array(
					 'options_titel' => $_POST['titelx' . $row['options_id']],
					 'options_text' => $_POST['textx' . $row['options_id']],
					 'options_width' => $_POST['widthx' . $row['options_id']],
					 'options_order' => $_POST['positionx' . $row['options_id']]
					);
					db::getInstance()->where('options_id', $row['options_id']);
					db::getInstance()->update('options', $data);
					break;
				case '4': // Option-List
					$data = Array(
					 'options_titel' => $_POST['titelx' . $row['options_id']],
					 'options_text' => $_POST['textx' . $row['options_id']],
					 'options_width' => $_POST['widthx' . $row['options_id']],
					 'options_order' => $_POST['positionx' . $row['options_id']]
					);
					db::getInstance()->where('options_id', $row['options_id']);
					db::getInstance()->update('options', $data);
					break;
			}
		}


		message::setNoteRow(_('Option successful saved.'), TRUE);
		$pagefile = 'module/page/' . $page . '/options/options.page.edit.php';
		if (is_file($pagefile))
			include_once $pagefile;
	}
}
else
{
	$pagefile = 'module/page/' . $page . '/options/options.page.edit.php';
	if (is_file($pagefile))
		include_once $pagefile;
}
?>